<?php

/**
 * @package		TSB
 * @license		GNU/GPL, see LICENSE.php
 */

jimport('joomla.application.component.controller');

/**
 * TSBControllerAssetsGroups
 *
 * @package		TSB
 */

/*
Component Naming Convention:
<Component Name><Controller><Controller Name>
* */
class TSBControllerUsers extends JController {
	/* constructor (registers additional tasks to methods)
	* @return void
	*/
	function __construct() {
		parent :: __construct();

		// Register Extra tasks
		$this->registerTask('add', 'edit');
	}

	function display() {
		if (!TSBRoles :: checkPermission($this, TSBConfig :: $VIEW_USERS, null, JText :: _('Vew Users').' - '.JText :: _('NO PERMISSION'))){
			return;
		}

		JRequest :: setVar('view', 'users');
		JRequest :: setVar('layout', 'list');

		parent::display();
	}

	/**
	 * display the edit form
	 * @return void
	 */
	function edit() {
		$task = JRequest :: getVar('task');

		switch ($task) {
			case 'add':		//Check Add Permission
				if (!TSBRoles :: checkPermission($this, TSBConfig :: $ADD_USERS, 'index.php?option=com_ju_tsb&controller=users', JText :: _('Add new Users').' - '.JText :: _('NO PERMISSION')))
					return;

				//Lay ve Roles Model
				$model = $this->getModel('users');
				//Lay ve All roles
				$system_users = $model->getAllSystemUsers();
				//Dua vao Request de chuyen toi View class
				JRequest :: setVar('system_users', $system_users);

				JRequest :: setVar('layout', 'add_form');	//Display HTML Add new Form
				break;
			case 'edit':	//Check Edit Permission
				if (!TSBRoles :: checkPermission($this, TSBConfig :: $EDIT_USERS, 'index.php?option=com_ju_tsb&controller=users', JText :: _('Edit Users').' - '.JText :: _('NO PERMISSION')))
					return;
				JRequest :: setVar('layout', 'edit_form');	//Display HTML Edit Form
				break;
		}

		JRequest :: setVar('view', 'users');
		JRequest :: setVar('hidemainmenu', 1);

		//Lay ve Roles Model
		$model = $this->getModel('roles');
		//Lay ve All roles
		$roles = $model->getAllData();
		//Dua vao Request de chuyen toi View class
		JRequest :: setVar('roles', $roles);

		parent :: display();
	}

	/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save() {
		$model = $this->getModel('users');

		if ($model->store($post)) {
			$msg = JText :: _('SAVED');
		} else {
			$msg = JText :: _('NOT SAVED');
		}

		// Check the table in so it can be edited.... we are done with it anyway
		$link = 'index.php?option=com_ju_tsb&controller=users&task=list';
		$this->setRedirect($link, $msg);
	}

	/**
	 * remove record(s)
	 * @return void
	 */
	function remove() {
		if (!TSBRoles :: checkPermission($this, TSBConfig :: $DEL_USERS, 'index.php?option=com_ju_tsb&controller=users', JText :: _('Delete Asset Groups').' - '.JText :: _('NO PERMISSION')))
			return;

		$model = $this->getModel('users');

		if (!$model->delete()) {
			$msg = JText :: _('NOT DELETED');
		} else {
			$msg = JText :: _('DELETED');
		}

		$this->setRedirect('index.php?option=com_ju_tsb&controller=users', $msg);
	}

	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel() {
		$msg = JText :: _('CANCELED');
		$this->setRedirect('index.php?option=com_ju_tsb&controller=users', $msg);
	}
}
?>
